###################################
# Title: The ‘Tough-on-Crime’ Left: Race, Gender, and
######## Elections of Law-and-Order Democrats
# Authors: Isabel Laterzo-Tingley  & Leah Christiani
# Date: May 2024

# File: Appendix 3 -- Survey on Student Sample
#####################################

#clear global environ
rm(list=ls())

#packages
library(tidyverse)
library(naniar)
library(cregg)
library(ggplot2)
library(scales)

#working rd
setwd("/Users/IsabelLaterzo/Dropbox/Research Projects/Tough-on-Crime, Race, Gender Paper - L. Christiani and I. Laterzo/Data/Replication Archive")

#read in data
data <- readRDS("survey2_data.rds")


#############################################################################
############################### DATA SET UP ################################
#############################################################################


# Isolate Democrats/Democrat Leaners
dem_data <- data %>% filter(dems == 1)

# Isolate Republicans/Republican Leaners
rep_data <- data %>% filter(reps == 1)


################################################################################
############################# CONJOINT RESULTS #################################
################################################################################


# Figure A32: Preferences for Crime Platforms by Respondent Partisanship 
############ (Student Sample)

base_model <- cj(data,
                 chosen ~ cand_pubsec,
                 id = ~id,
                 estimate = "mm",
                 alpha = .1)

dem_model <- cj(dem_data,
                chosen ~ cand_pubsec,
                id = ~id,
                estimate = "mm",
                alpha = .1)

rep_model <- cj(rep_data,
                chosen ~ cand_pubsec,
                id = ~id,
                estimate = "mm",
                alpha = .1)


### combining
base_model$sample <- "Pooled"
dem_model$sample <- "Democrats"
rep_model$sample <- "Republicans"
base_pooled <- rbind(base_model, dem_model, rep_model)
base_pooled$sample <- factor(base_pooled$sample, 
                             levels = c("Pooled", "Democrats",
                                        "Republicans"),
                             ordered = T)





fig32_app <- ggplot(base_pooled, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) + 
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.48, 0.5, 0.52)) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"),
        panel.spacing = unit(1, "lines"))

fig32_app 

#ggsave("pssp_fig1_plot.png", fig32_app , width=8, height=3)


# Figure A33: Preferences for Crime Platforms by Respondent Partisanship and
############ Candidate Race (Student Sample)

base_interact_race <- cj(data,
                         chosen ~ cand_pubsec,
                         id = ~id,
                         estimate = "mm",
                         by = ~ cand_race,
                         alpha = .1)

dem_interact_race <- cj(dem_data,
                        chosen ~ cand_pubsec,
                        id = ~id,
                        estimate = "mm",
                        by = ~ cand_race,
                        alpha = .1)

rep_interact_race <- cj(rep_data,
                        chosen ~ cand_pubsec,
                        id = ~id,
                        estimate = "mm",
                        by = ~ cand_race,
                        alpha = .1)


### combining
base_interact_race$sample <- "Pooled"
dem_interact_race$sample <- "Democrats"
rep_interact_race$sample <- "Republicans"
race_pooled <- rbind(base_interact_race, dem_interact_race,
                     rep_interact_race)
race_pooled$sample <- factor(race_pooled$sample, 
                             levels = c("Pooled", "Democrats",
                                        "Republicans"),
                             ordered = T)




fig33a_app <- ggplot(race_pooled, aes(x = level, y = estimate, linetype = cand_race)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.46, 0.5, 0.54)) +
  scale_linetype_discrete(name="Candidate Race",
                          labels =c("Black", "White")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig33a_app


#ggsave("pssp_fig2a_plot.png", fig33a_app, width=8, height=3)




# differences in MMs
diff_base_interact_race <- cj(data,
                              chosen ~ cand_pubsec,
                              id = ~id,
                              estimate = "mm_diff",
                              by = ~ cand_race,
                              alpha = .1)


diff_dem_interact_race <- cj(dem_data,
                             chosen ~ cand_pubsec,
                             id = ~id,
                             estimate = "mm_diff",
                             by = ~ cand_race,
                             alpha = .1)

diff_rep_interact_race <- cj(rep_data,
                             chosen ~ cand_pubsec,
                             id = ~id,
                             estimate = "mm_diff",
                             by = ~ cand_race,
                             alpha = .1)

### combining diff
diff_base_interact_race$sample <- "Pooled"
diff_dem_interact_race$sample <- "Democrats"
diff_rep_interact_race$sample <- "Republicans"
race_diff_total <- rbind(diff_base_interact_race, diff_dem_interact_race,
                         diff_rep_interact_race)
race_diff_total$sample <- factor(race_diff_total$sample, 
                                 levels = c("Pooled", "Democrats",
                                            "Republicans"),
                                 ordered = T)



fig33b_app <- ggplot(race_diff_total, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.3)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .1,
                alpha = 0.8,
                position=position_dodge(0.3),
                lwd = 0.7) +
  facet_grid(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01)) +
  labs(x = "Crime Policy",
       y = "Difference in Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14))

fig33b_app

#ggsave("pssp_fig2b_plot.png", fig33a_app, width=8, height=2)



# Figure A34: Preferences for Crime Platforms by Respondent Partisanship and
############ Candidate Gender (Student Sample)


base_interact_gen <- cj(data,
                        chosen ~ cand_pubsec,
                        id = ~id,
                        estimate = "mm",
                        by = ~ cand_gender,
                        alpha = .1)

dem_interact_gen <- cj(dem_data,
                       chosen ~ cand_pubsec,
                       id = ~id,
                       estimate = "mm",
                       by = ~ cand_gender,
                       alpha = .1)

rep_interact_gen <- cj(rep_data,
                       chosen ~ cand_pubsec,
                       id = ~id,
                       estimate = "mm",
                       by = ~ cand_gender,
                       alpha = .1)


### combining
base_interact_gen$sample <- "Pooled"
dem_interact_gen$sample <- "Democrats"
rep_interact_gen$sample <- "Republicans"
gender_pooled <- rbind(base_interact_gen, dem_interact_gen,
                       rep_interact_gen)
gender_pooled$sample <- factor(gender_pooled$sample, 
                               levels = c("Pooled", "Democrats",
                                          "Republicans"),
                               ordered = T)




fig34a_app <- ggplot(gender_pooled, aes(x = level, y = estimate, linetype = cand_gender)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.46, 0.5, 0.54)) + 
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig34a_app


#ggsave("pssp_fig3a_plot.png", fig3a, width=8, height=3)




# differences in MMs
diff_base_interact_gen <- cj(data,
                             chosen ~ cand_pubsec,
                             id = ~id,
                             estimate = "mm_diff",
                             by = ~ cand_gender,
                             alpha = .1)


diff_dem_interact_gen <- cj(dem_data,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "mm_diff",
                            by = ~ cand_gender,
                            alpha = .1)

diff_rep_interact_gen <- cj(rep_data,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "mm_diff",
                            by = ~ cand_gender,
                            alpha = .1)

### combining diff
diff_base_interact_gen$sample <- "Pooled"
diff_dem_interact_gen$sample <- "Democrats"
diff_rep_interact_gen$sample <- "Republicans"
gen_diff_total <- rbind(diff_base_interact_gen, diff_dem_interact_gen,
                        diff_rep_interact_gen)
gen_diff_total$sample <- factor(gen_diff_total$sample, 
                                levels = c("Pooled", "Democrats",
                                           "Republicans"),
                                ordered = T)



fig34b_app <- ggplot(gen_diff_total, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.3)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .1,
                alpha = 0.8,
                position=position_dodge(0.3),
                lwd = 0.7) +
  facet_grid(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01)) +
  labs(x = "Crime Policy",
       y = "Difference in Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14))

fig34b_app

#ggsave("pssp_fig3b_plot.png", fig34b_app, width=8, height=2)



